﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlotasDesktop;
using System.Data.SqlClient;
using System.Globalization;

namespace GestorDeFlotasDesktop.AbmChofer
{
    public partial class AltaChofer : Form
    {
        String conexion;
        public AltaChofer()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
        }

        private void altaButton_Click(object sender, EventArgs e)
        {
            if (nombreTextBox.Text != string.Empty && apellidoTextBox.Text != string.Empty && direccionTextBox.Text != string.Empty && telefonoTextBox.Text != string.Empty && dniTextBox.Text != string.Empty)
            {
                if (this.dniLargoCorrecto(dniTextBox.Text) && this.telefonoLargoCorrecto(telefonoTextBox.Text))
                    this.altaChoferSQL();
                else
                {
                    MessageBox.Show("El telefono y el dni solo aceptan 8 digitos");
                }
            }
            else { MessageBox.Show("Todos los campos a excepcion del mail son obligatorios");}
        }

       /* private void dniTextBox_KeyPress(object sender, KeyPressEventArgs e)
         {
            if (Char.IsDigit(e.KeyChar)){
                e.Handled = false;
            }
                else if (Char.IsControl(e.KeyChar)){
                    e.Handled = false;
                }
                    else if (Char.IsSeparator(e.KeyChar)){
                        e.Handled = false;
                    }
                        else{
                            e.Handled = true;
                        }
        }*/
        public void altaChoferSQL()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        int telefono = Convert.ToInt32(telefonoTextBox.Text);
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_altaChofer";
                        command.Parameters.AddWithValue("@Nombre", nombreTextBox.Text);
                        command.Parameters.AddWithValue("@Apellido", apellidoTextBox.Text);
                        command.Parameters.AddWithValue("@DNI",Convert.ToInt32(dniTextBox.Text));
                        command.Parameters.AddWithValue("@Telefono", Convert.ToInt32(telefonoTextBox.Text));
                        command.Parameters.AddWithValue("@Direccion",direccionTextBox.Text);
                        command.Parameters.AddWithValue("@Mail", mailTextBox.Text);
                        command.Parameters.AddWithValue("@Fecha_Nacimiento", fechaNacimientoMonthCalendar.SelectionStart);
                        command.Parameters.AddWithValue("@Habilitado", true);
                        command.ExecuteReader();
                        MessageBox.Show("Operacion realizada con exito");
                        connection.Close();
                        this.Close();
                                       
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dniTextBox_TextChanged(object sender, EventArgs e)
        {
       
        }

        private Boolean dniLargoCorrecto(String dni){
            return (dni.Length <= 8);
        }

        private Boolean telefonoLargoCorrecto(String dni)
        {
            return (dni.Length <= 8);
        }


        private void dniTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar) || Char.IsSeparator(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
